import Weapon from "./weapon.js"
import WaveBullet from "./wave_bullet.js"
import {calc_distance} from "./utils.js"

class WaveWeapon extends Weapon {
    constructor(game, row, col, config) {
        super(game, row, col, config);

        this.target_spirit = null;

        if (!WaveWeapon.hasOwnProperty("weapon_frames")) {
            WaveWeapon.weapon_frames = [this.bg_draw(), this.bg_draw(2)];
        }

        this.bullet_index = 0;
    }

    fire() {
        let ts = new Date().getTime();
        if (this.ts && ts - this.ts < this.config.fire_interval) return;
        this.ts = ts;
        this.bullet_index = (this.bullet_index + 1) % 2;

        let position = this.map_cell.get_center_xy();
        let spirit = this.find_spirit();
        if (spirit) {
            this.bullet_list.push(new WaveBullet(this, position, 0, this.config, this.bullet_index));
            this.fire_duration += this.config.fire_interval;
        }
    };

    bullet_check_hit() {
        let ts = new Date().getTime();
        if (this.ts2 && ts - this.ts2 < this.config.fire_interval) return;
        this.ts2 = ts;

        let spirit_list = this.find_spirit(true);
        if (!spirit_list) return;

        for (let i = 0; i < spirit_list.length; i++) {
            let spirit = spirit_list[i];
            let spirit_pos = spirit.position;
            let weapon_pos = this.map_cell.get_center_xy();
            let xy = calc_distance(spirit_pos, weapon_pos);
            let power = this.config.bullet_power * Math.sqrt(1 - (xy / this.config.fire_range)) | 0;
            power = power || 1;
            // let power = this.config.bullet_power * (1 - (xy / this.config.fire_range)) | 0;
            if (spirit.blood >= 0) {
                spirit.blood -= power;
                this.score += power;
            }
        }
    };
}
export default WaveWeapon;